﻿package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.events.SampleDataEvent;
	import flash.display.LoaderInfo;
	
	/**
	 * ...
	 * @author andyd
	 */
	
	
	 
	public class Main extends Sprite 
	{
		//Create an instance of the Sound class
		private var soundClip:Sound;
			//Create a new SoundChannel Object
		private var sndChannel:SoundChannel;
			
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{

			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			
			
			
			
			sounds = new Array();
			soundChannels = new Array();
			
			loadCount = 5;
			
			/////////////////////
			
			soundClip = new Sound();
			sndChannel = new SoundChannel();
			
			soundClip.load(new URLRequest(String(getFlashVars().progressionSample)));// "http://www.extlabs.com/canopy/sites/default/files/mysz4.mp3"));
			soundClip.addEventListener(Event.COMPLETE,loadSample,false,0,true);
			
			sounds.push(soundClip);
			soundChannels.push(sndChannel);
			
			soundClip = new Sound();
			sndChannel = new SoundChannel();
			
			soundClip.load(new URLRequest(String(getFlashVars().progression2Sample)));
			soundClip.addEventListener(Event.COMPLETE,loadSample,false,0,true);
			
			sounds.push(soundClip);
			soundChannels.push(sndChannel);
			
			soundClip = new Sound();
			sndChannel = new SoundChannel();
			
			soundClip.load(new URLRequest(String(getFlashVars().drumsSample)));
			soundClip.addEventListener(Event.COMPLETE,loadSample,false,0,true);
			
			sounds.push(soundClip);
			soundChannels.push(sndChannel);
			
			soundClip = new Sound();
			sndChannel = new SoundChannel();
			
			soundClip.load(new URLRequest(String(getFlashVars().drums2Sample)));
			soundClip.addEventListener(Event.COMPLETE,loadSample,false,0,true);
			
			sounds.push(soundClip);
			soundChannels.push(sndChannel);
			
			
			loadCount--;
		}
		
		private var loadCount:int;
		private var sounds:Array;
		private var soundChannels:Array;
		
		private var BUFFER_SIZE:int = 7350;
		
		private var buffer:Vector.<ByteArray> = new Vector.<ByteArray>(4);
		private var sound:Vector.<Sound> = new Vector.<Sound>(4);
		private var lengths:Vector.<int> = new Vector.<int>(4);
		private var volumes:Vector.<Number> = new Vector.<Number>(4);
		
		private function nearestPowerOfTwo(i:int):int
		{

			   var x:int = ((i - 1) & i);

			   return x ? nearestPowerOfTwo(x) : i << 1;

			
		}   
		
		private function onSampleData(sampleDataEvent:SampleDataEvent) : void
		{
		  for (var i:int = 0; i <  4; i++) {
			buffer[i].position = 0;
			

				if(sampleDataEvent.position/BUFFER_SIZE%lengths[i]==0)
					sound[i].extract(buffer[i], BUFFER_SIZE,0); 
				else
					sound[i].extract(buffer[i], BUFFER_SIZE);

			
			
			buffer[i].position = 0;
			
			if (i == 0)
			{
				//if((sampleDataEvent.position/7350)%32==0)
					//trace(sndCChannel.position);
			//trace(String("sample " + (sampleDataEvent.position/7350)));
			//trace(String("sound " + (sound[i].length/180)));
			}
			//sampleDataEvent.position
			//Sound(sound[i])
			
		  }

		  for (var j:int = 0; j < BUFFER_SIZE*2; j++) 
		  {
			var val:Number = 0;
			for (var k:int = 0; k < 4; k++) 
			{
			  val += buffer[k].readFloat()*volumes[k];
			}
			sampleDataEvent.data.writeFloat(val);
			
		  }
		}
		
			public function loadSample(evt:Event):void 
			{
				//Play loaded sound
				
				//sndChannel = soundClip.play();
				
				if (--loadCount <= 0)
					playAll();
			}
			
			private function getFlashVars():Object {
				return Object( LoaderInfo( this.loaderInfo ).parameters );
			}

			
			public function playAll():void
			{
				/*
				for (var a:int = 1; a < soundChannels.length; a++)
				{
					soundChannels[a] = Sound(sounds[a]).play();
				}*/
				
				sound[0] = sounds[0];
				sound[1] = sounds[1];
				sound[2] = sounds[2];
				sound[3] = sounds[3];
				
				
				trace(sound[0].length);
				trace(sound[1].length);
				trace(sound[2].length);
				trace(sound[3].length);
				
				
				lengths[0] = nearestPowerOfTwo(int(sound[0].length / 168.16326530612244))/2;
				lengths[1] = nearestPowerOfTwo(int(sound[1].length / 168.16326530612244))/2;
				lengths[2] = nearestPowerOfTwo(int(sound[2].length / 168.16326530612244))/2;
				lengths[3] = nearestPowerOfTwo(int(sound[3].length / 168.16326530612244))/2;
				
				trace(lengths[0]);
				trace(lengths[1]);
				trace(lengths[2]);
				trace(lengths[3]);
				
				volumes[0] = Number(getFlashVars().sample1volume)/100;
				volumes[1] = Number(getFlashVars().sample2volume)/100;
				volumes[2] = Number(getFlashVars().sample3volume)/100;
				volumes[3] = Number(getFlashVars().sample4volume)/100;
				
				buffer[0] = new ByteArray();
				buffer[1] = new ByteArray();
				buffer[2] = new ByteArray();
				buffer[3] = new ByteArray();
				
				var soundC:Sound = new Sound();
				soundC.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
				
				sndCChannel = new SoundChannel();
				sndCChannel = soundC.play();
				
				
				
			}
			
			private var sndCChannel:SoundChannel;
		
		
	}
	
}